/**
 * @project readnote
 * @Title SellSelfMailServiceImpl.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.sellselfmail.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.mail.internet.MimeMessage;

import com.cr.backbone.dao.business.sysfile.SysFileDAO;
import com.cr.backbone.hibenate.AppBaseServiceImpl;
import com.cr.backbone.model.SysFile;
import com.cr.backbone.util.HTMLMessage;
import com.cr.backbone.util.UtilsDataDict;
import com.cr.backbone.util.UtilsDateTime;
import com.cr.backbone.util.UtilsSendMail;
import com.cr.backbone.util.UtilsThreadPoolSendMail;
import com.readnote.sellselfmail.dao.SellSelfMailDAO;
import com.readnote.sellselfmail.model.SellSelfMail;
import com.readnote.sellselfmail.service.SellSelfMailService;

public class SellSelfMailServiceImpl extends AppBaseServiceImpl<SellSelfMail> implements SellSelfMailService {

	private SysFileDAO sysFileDAO;
	
	public SellSelfMailDAO getDAO(){
		return ((SellSelfMailDAO)(dao));
	}
	
	

	
	/**
	 * 保存申请记录
	 * @param sellSelfMail
	 * @return
	 * @author huxh
	 * @date 2012-7-2
	 */
	public Long saveSellSelfMail(SellSelfMail sellSelfMail) {
		Long id=null;
		Integer frequency=60;
		Integer sendCount=0;
		
		sellSelfMail.setCreateDate(UtilsDateTime.getCurrentDate());
		sellSelfMail.setStatus(UtilsDataDict.SELL_SELF_MAIL_STATUS_APPLY);
		sellSelfMail.setSendDate(UtilsDataDict.SELL_SELF_MAIL_SEND_DATE_WORK);
		sellSelfMail.setFrequency(frequency);
		sellSelfMail.setSendCount(sendCount);
		
		id = getDAO().save(sellSelfMail);
		
		return id;
	}


	/**
	 * 执行发送，并更新累计次数
	 * @param sellSelfMail
	 * @return
	 * @author huxh
	 * @date 2012-7-2
	 */
	public Long updateSellSelfMailCount(SellSelfMail sellSelfMail) {
		Long id=sellSelfMail.getId();
		SellSelfMail _sellSelfMail=getDAO().get(id);
		
		_sellSelfMail.setSendCount(_sellSelfMail.getSendCount()+1);//累计次数
		
		getDAO().update(_sellSelfMail);
		
		return id;
	}
	
	/**
	 * 定时发送简历
	 * 	1、查询处于申请中明细
	 *  2、检测是否符合当前条件
	 *  3、执行发送
	 * @author huxh
	 * @date 2012-7-2
	 */
	public void saveSendSellSelfMail() {
		
		//0、工作日才执行检查、并发送邮件
		if(!isWorkDay())
			return ;
		
		//1、查询处于申请中明细
		List<SellSelfMail> sellSelfMails  =getDAO().getListByApply();
		
		//2、检测是否符合当前条件
		List<SellSelfMail> sends=new ArrayList<SellSelfMail>();
		for(SellSelfMail sell : sellSelfMails){
			if( canSendSell(sell) )
				sends.add(sell);
			//TODO 2012-06-29 huxh test all。不管是否符合条件的申请中，都会执行发送
			sends.add(sell);
		}
		
		
		//3、执行批量发送
		sendMialsBySells(sends);
		
	}
	
	/**
	 * 
	 * @return
	 * @author huxh
	 * @date 2012-7-2 
	 */
	public boolean isWorkDay() {
		Calendar c =java.util.Calendar.getInstance();
		c.setTime(new Date());
		Integer day_of_week=c.get(c.DAY_OF_WEEK);
		Integer hour = UtilsDateTime.getCurrentHour();
		
		if(( day_of_week>=0 &&day_of_week<=4) && ( hour >=9 && hour <=18 )  ){
			//日：周一至周五
			//小时 ：09:00-18:00
			return true;
		}
		
		return false;
		
	}


	/**
	 * 
	 * @param sends
	 * @author huxh
	 * @date 2012-7-2 
	 */
	public void sendMialsBySells(List<SellSelfMail> sends) {
		for(SellSelfMail sell : sends){
			
			try {
				//1 邮件发送
				String from="enjoyreadnote@163.com";
				String to=sell.getHumanResourcesMail();
				String subject=sell.getCompanyTitle();
				String text="您好，这是一封应聘职务邮件。file Id:"+sell.getSysFile().getId()+" file Name:"+sell.getSysFile().getFileName();
//				UtilsThreadPoolSendMail.getInstance().sendMail(from, to, subject, text);
				
				text="<html><body><div>你好</div><h1>HTML邮件</h1><h2>无附件</h2></body></html>";
//				UtilsThreadPoolSendMail.getInstance().sendHTMLMail(from, to, subject, text);
				
				String h="http://localhost:8888/center/reply/close.do?sid="+sell.getId()+"&hrEmail="+sell.getHumanResourcesMail()+"";
				text="<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\"></head><body><div>你好</div><h1>HTML邮件</h1><h2>包含附件</h2><a href=\""+h+"\">已经收到简历，取消简历订阅</a></body></html>";
//				text="<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\"></head><body><div>你好</div><h1>HTML邮件</h1><h2>包含附件</h2>超链接测试是否可以用</body></html>";
				MimeMessage mimeMessageHtml=UtilsSendMail.getInstance().initMimeMailMessage(from, to, subject, text);
				
				
				SysFile sysFile=sell.getSysFile();
				//SysFile sysFile=sysFileDAO.get(37l);
				//mimeMessageHtml = UtilsSendMail.getInstance().initMimeMailMessageAddAttachment(mimeMessageHtml, sysFile);
				HTMLMessage htmlMess=new HTMLMessage();
				mimeMessageHtml = htmlMess.setFrom(from).setTo(to).setSubject(subject).setText(text).addAtt(sysFile).getMimeMessage();
				UtilsThreadPoolSendMail.getInstance().sendHTMLMail(mimeMessageHtml);
				
				
				
				//2 更新状态
				updateSellSelfMailCount(sell);
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
	}


	/**
	 * 是否符合发送条件
	 * @param sell
	 * @return
	 * @author huxh
	 * @date 2012-7-2 
	 */
	public boolean canSendSell(SellSelfMail sell) {
		
		if(sell !=null && sell.getStatus().equals(UtilsDataDict.SELL_SELF_MAIL_STATUS_APPLY)){
			Integer minute=UtilsDateTime.getCurrentMinute();
			int f=sell.getFrequency();
			
			int result = minute % f;
			
			if(( isWorkDay() ) &&( minute == 0 || result ==0) ){
				//日：周一至周五
				//小时 ：09:00-18:00
				//分钟：处于0分钟，或者当前分钟处于频率中：例如当前为15分，那么频率为5分钟可执行发送=15%5
				return true;
			}
			
		}
		
		return false;
	}


	/**
	 * 更新频率
	 * @author huxh
	 * @date 2012-7-2
	 */
	public void updateSendSellFace() {
		//1、查询处于申请中明细
		
		//2、
	}
	
	/**
	 * 
	 * 
	 * @param sellSelfMail
	 * @author huxh
	 * @date 2012-7-2
	 */
	public void updateSellSendClose(SellSelfMail sellSelfMail){
		SellSelfMail _sellSelfMail=getDAO().get(sellSelfMail.getId());
		
		_sellSelfMail.setStatus(UtilsDataDict.SELL_SELF_MAIL_STATUS_CLOSE);//接收完毕
		_sellSelfMail.setReply(sellSelfMail.getReply());//回复意见
		
		getDAO().update(_sellSelfMail);
	}
	
	/**
	 * 检测是否，当前操作者为人事经理
	 * @param sellSelfMail
	 * @param email
	 * @return
	 * @author huxh
	 * @date 2012-7-2
	 */
	public boolean isHumanResourcesMail(SellSelfMail sellSelfMail,String email) {
		SellSelfMail _sellSelfMail = getDAO().get(sellSelfMail.getId());
		if(email!=null && !email.equals("") && email.equals(_sellSelfMail.getHumanResourcesMail()))
			return true;
		return false;
	}

	public SysFileDAO getSysFileDAO() {
		return sysFileDAO;
	}

	public void setSysFileDAO(SysFileDAO sysFileDAO) {
		this.sysFileDAO = sysFileDAO;
	}
	
}
